Imports NamesDataAccess
Imports TaxonNamesInterface
Imports System.Data.OleDb

Public Class BrAuthors
    Implements TaxonNamesInterface.IAuthors

    Private Shared _authorData As IAuthorData

    Private Shared Function AuthorInterface() As IAuthorData
        If _authorData Is Nothing Then _authorData = New AuthorData
        Return _authorData
    End Function

    'returns as string of the corrected author ids
    ' eg 'Smith & Jones' passed in, then something like '1044 1662' is returned
    Public Function ParseAuthors(ByVal authors As String) As String Implements TaxonNamesInterface.IAuthors.ParseAuthors
        Dim result As String = ""

        Dim origAuthors As String = authors

        authors = authors.ToLower()

        'remove before brackets
        If authors.LastIndexOf(")") <> -1 Then
            authors = authors.Substring(authors.LastIndexOf(")") + 1)
        End If

        'remove 'in citation', 'ex' etc

        While authors.IndexOf("  ") <> -1
            authors = authors.Replace("  ", " ")
        End While

        authors = authors.Replace(" et ", " & ")

        If authors.IndexOf(" ex ") <> -1 Then
            authors = authors.Substring(authors.IndexOf(" ex ") + 4)
        End If

        If authors.IndexOf(" in ") <> -1 Then
            authors = authors.Substring(0, authors.IndexOf(" in "))
        End If

        authors = authors.Trim

        If authors.Length > 0 Then
            'split by '&', ','
            Dim endpos1 As Integer = authors.IndexOf("&")
            Dim endpos2 As Integer = authors.IndexOf(",")
            Dim endpos As Integer = Math.Min(endpos1, endpos2)
            If endpos = -1 Then endpos = Math.Max(endpos1, endpos2)
            Dim pos As Integer = 0

            While pos <> -1
                Dim auth As String = ""
                Dim origAuth As String = ""
                If endpos = -1 Then
                    auth = authors.Substring(pos).Trim
                    origAuth = origAuthors.Substring(pos).Trim
                Else
                    auth = authors.Substring(pos, endpos - pos).Trim
                    origAuth = origAuthors.Substring(pos, endpos - pos).Trim
                End If

                If auth.Length > 0 Then
                    Dim authId As Integer = -1
                    Dim a As TaxonNamesInterface.Author = GetAuthorByName(auth)
                    If a IsNot Nothing Then
                        authId = a.CorrectAuthorId
                    Else
                        'insert new author
                        a = New TaxonNamesInterface.Author
                        a.Abbreviation = origAuth
                        AuthorInterface.SaveAuthor(a, SessionState.CurrentUser.Login)
                        authId = a.AuthorId
                    End If

                    result += authId.ToString + " "
                End If

                If endpos = -1 Then
                    pos = -1
                Else
                    pos = endpos + 1
                    endpos1 = authors.IndexOf("&", pos)
                    endpos2 = authors.IndexOf(",", pos)
                    endpos = Math.Min(endpos1, endpos2)
                    If endpos = -1 Then endpos = Math.Max(endpos1, endpos2)
                End If

            End While
        End If

        Return result.Trim
    End Function

    'find any ex authors in the authors string
    Public Function GetExAuthors(ByVal authors As String) As String Implements TaxonNamesInterface.IAuthors.GetExAuthors
        Dim auth As String = ""

        authors = authors.ToLower
        'remove preceding basionym authors ?
        Dim brPos As Integer = authors.LastIndexOf(")")
        If brPos <> -1 Then
            authors = authors.Substring(brPos + 1)
        End If

        If authors.IndexOf(" ex ") <> -1 Then
            Dim exAuth As String = authors.Substring(0, authors.IndexOf(" ex "))

            auth = ParseAuthors(exAuth)
        End If

        Return auth
    End Function

    'list of space separated ids passed in, eg 14423 66332
    'list of corrected ids passed back, eg 14888 66332
    Public Function GetCorrectedAuthors(ByVal authorIdList As String) As String Implements TaxonNamesInterface.IAuthors.GetCorrectedAuthors
        Dim corrIds As String = ""

        If authorIdList.Length > 0 Then
            Dim endpos As Integer = authorIdList.IndexOf(" ")
            Dim pos As Integer = 0

            While pos <> -1
                Dim id As String = ""
                If endpos = -1 Then
                    id = authorIdList.Substring(pos).Trim
                Else
                    id = authorIdList.Substring(pos, endpos - pos).Trim
                End If

                If id.Length > 0 Then
                    Dim a As TaxonNamesInterface.Author = AuthorInterface.GetAuthor(id)
                    If a IsNot Nothing Then
                        corrIds += a.CorrectAuthorId.ToString + " "
                    End If
                End If

                If endpos = -1 Then
                    pos = -1
                Else
                    pos = endpos + 1
                    endpos = authorIdList.IndexOf(" ", pos)
                End If

            End While
        End If

        Return corrIds.Trim
    End Function

    'lists of space delimited corrected author ids passed in
    'fullName is true if the name should be formatted for a full name
    Public Function GetFullAuthorString(ByVal basAuth As String, ByVal combAuth As String, ByVal basEx As String, ByVal combEx As String, ByVal fullName As Boolean) As String Implements TaxonNamesInterface.IAuthors.GetFullAuthorString
        Dim basAuthStr As String = ""
        Dim combAuthStr As String = ""


        If combAuth.Length > 0 Then
            combAuthStr = GetAuthorString(combAuth)
            If combEx.Length > 0 Then
                combAuthStr = GetAuthorString(combEx) + " ex " + combAuthStr
            End If
        End If

        If basAuth.Length > 0 Then
            basAuthStr = GetAuthorString(basAuth)
            If basEx.Length > 0 Then
                basAuthStr = GetAuthorString(basEx) + " ex " + basAuthStr
            End If
        End If

        Dim authorStr As String = ""

        If basAuthStr.Length > 0 Then
            If fullName Then
                authorStr = "(" + basAuthStr + ")"
            Else
                authorStr = basAuthStr
            End If
            If combAuthStr.Length > 0 Then authorStr += " "
        End If
        If combAuthStr.Length > 0 Then authorStr += combAuthStr

        Return authorStr
    End Function

    'list of space delimited ids passed in, eg 2432 7642
    'author string passed back
    Public Function GetAuthorString(ByVal authors As String) As String Implements TaxonNamesInterface.IAuthors.GetAuthorString
        Dim authorStr As String = ""

        Dim endpos As Integer = authors.IndexOf(" ")
        Dim pos As Integer = 0

        Dim delim As String = ""

        While pos <> -1
            Dim id As String = ""
            If endpos = -1 Then
                id = authors.Substring(pos).Trim
            Else
                id = authors.Substring(pos, endpos - pos).Trim
            End If

            If id.Length > 0 Then
                Dim a As TaxonNamesInterface.Author = AuthorInterface.GetAuthor(id)
                If a IsNot Nothing AndAlso a.Abbreviation.Length > 0 Then
                    authorStr += delim + a.Abbreviation
                    delim = ", " 'after first one
                End If
            End If

            If endpos = -1 Then
                pos = -1 'end
                'replace last ,
                Dim lastPos As Integer = authorStr.LastIndexOf(",")
                If lastPos <> -1 Then authorStr = authorStr.Substring(0, lastPos) + " &" + authorStr.Substring(lastPos + 1)
            Else
                pos = endpos + 1
                endpos = authors.IndexOf(" ", pos)
            End If
        End While

        Return authorStr
    End Function

    Public Function GetAuthorByName(ByVal author As String) As TaxonNamesInterface.Author Implements TaxonNamesInterface.IAuthors.GetAuthorByName
        Return AuthorInterface.GetAuthorByName(author)
    End Function

    Public Function GetAuthor(ByVal authorId As Integer) As TaxonNamesInterface.Author Implements TaxonNamesInterface.IAuthors.GetAuthor
        Return AuthorInterface.GetAuthor(authorId)
    End Function

    Public Function ListAuthors(ByVal searchText As String, ByVal anywhereInText As Boolean) As List(Of TaxonNamesInterface.Author) Implements TaxonNamesInterface.IAuthors.ListAuthors
        Return AuthorInterface.ListAuthors(searchText, anywhereInText)
    End Function

    Public Sub SaveAuthor(ByVal auth As TaxonNamesInterface.Author, ByVal user As String) Implements TaxonNamesInterface.IAuthors.SaveAuthor
        AuthorInterface.SaveAuthor(auth, user)
    End Sub


End Class
